home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Aminet 31
/
Aminet 31 (1999)(Schatztruhe)[!][Jun 1999].iso
/
Aminet
/
dev
/
c
/
GAPLib.lha
/
GAPLib_Beta
/
wizards
/
Conjurer.c
< prev
next >
Wrap
C/C++ Source or Header
|
1999-04-22
|
35KB
|
1,658 lines
/*
* The GAP Conjurer (C)1999 Peter Bengtsson
*
* History:
*
* 22/4-1999: v1.0
*
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <time.h>
#include <sys/stat.h>
#include <errno.h>
#include "data/bs_c.c"
#include "data/bm_c.c"
#include "data/bv_c.c"
#include "data/sk_c.c"
#include "data/report_h.c"
#include "data/report_c.c"
#include "data/mk.c"
const char *__v__="$VER: GAP-Conjurer 1.0 (22.4.99) ©1999 Peter Bengtsson";
/* #define CLEAR "\x1b" "c" */
#define CLEAR "\x1b[H\x1b[J"
#define TF(x) ((x)?"TRUE":"FALSE")
#define DIR_SEPARATOR '/'
#ifdef AMIGA
#define CURRENT_DIR ""
#else
#define CURRENT_DIR "."
#endif
#define Rnd(a) (rand()%a)
#define InitRand(a) srand((unsigned int)a)
#define YES 'Y'
#define NO 'N'
#define DEFY "[Y/n]"
#define DEFN "[y/N]"
#define MN_IGN 0
#define MN_STR 1
#define MN_INT 2
#define MN_BOL 3
#define WIZFLG_PLOT (1<<0)
#define WIZFLG_PSPLOT (1<<1)
#define WIZFLG_MPASS (1<<2)
#define DEF_GENERATIONS 10
#define DEF_BRVLEN 3
#define DEF_NAME "Manhattan"
#define NUM_DEFNAMES 11
char *includes[] = {
"stdio",
"stdlib",
"string",
"math",
"time",
"GAP",
NULL
};
char *SelectNames[] = {
"Double Random",
"Fitness Proportionate",
"Sigma Scaled",
"Tournament",
"Inorder",
"Boltzmann",
"Universal",
NULL
};
char *RepDefs[] = {
"AVERAGE",
"MEDIAN",
"TYPECOUNT",
"MAX",
"MIN",
"STDDEV"
};
char *RepNames[] = {
"Average",
"Median",
"Typecount",
"Maximum",
"Minimum",
"Standard Deviation"
};
char *RepExts[] = {
"avg",
"med",
"typ",
"max",
"min",
"dev"
};
char *SelectDefs[] = {
"DRANDOM",
"FITPROP",
"SIGMA",
"TOURNAMENT",
"INORDER",
"TEMPERATURE",
"UNIVERSAL"
};
char *CrossoverNames[] = {
"Singlepoint",
"Multipoint",
"Custom",
NULL
};
char *InitNames[] = {
"Random init",
"Zero init",
"Custom init",
NULL
};
char *CLNames[] = {
"None",
"Normal",
"Verbose",
"Irritating",
"Overwhelming",
NULL
};
char **CLMenu = CLNames;
#define PTYPE_Bitstring 0
#define PTYPE_Bitmatrix 1
#define PTYPE_BRVector 2
#define PTYPE_Custom 3
char *GenomeNames[] = {
"Bitstring",
"Bitmatrix",
"Bounded double vector",
"Custom",
NULL
};
char **GenomeMenu = GenomeNames;
char *DefNames[] = {
"Tourist",
"Hacker",
"Gnome",
"Borg",
"Bug",
"Zool",
"Ghost",
"Hobbit",
"Haddock",
"Dinosaur",
"Polyphant",
NULL
};
struct VDisp {
int Type;
long Val;
};
char *Menu1[] = {
"Project parameters",
"Population parameters",
"Make project",
"Save configuration",
"Load configuration",
"Quit",
NULL
};
char *PrParms[] = {
"Project name",
"No. of populations",
"Comment level",
"Multiple sourcefiles",
"GNUPlot script",
"PostScript plots",
"Exit",
NULL
};
struct VDisp PrVals[] = {
{MN_STR,0},
{MN_INT,0},
{MN_STR,0},
{MN_BOL,0},
{MN_BOL,0},
{MN_BOL,0},
{MN_IGN,0}
};
char *PoParms[] = {
"Change name",
"Change size",
"Genome type",
"Environment parameters",
"Report parameters",
"Exit",
NULL
};
struct VDisp PoVals[] = {
{MN_STR,0},
{MN_INT,0},
{MN_STR,0},
{MN_IGN,0},
{MN_IGN,0},
{MN_IGN,0}
};
char *Flags[] = {
"Init type",
"Selection method",
"Crossover",
"Crowding",
"Mutation",
"Pre-mutate",
"Elitism",
"Dump worst",
"Init dumped",
"Newbies",
"Erase best",
"Make stats",
"Exit",
NULL
};
char *FlagTags[] = {
"EVL_Evaluator",
"EVL_Select",
"EVL_Crosser",
"EVL_Crowding",
"EVL_Mutator",
"EVL_PreMutate",
"EVL_Elite",
"EVL_Dump",
"EVL_InitDumped",
"EVL_Newbies",
"EVL_EraseBest",
"EVL_Stats"
};
struct VDisp FlagVals[] = {
{MN_STR,0},
{MN_STR,0},
{MN_STR,0},
{MN_BOL,0},
{MN_BOL,0},
{MN_BOL,0},
{MN_INT,0},
{MN_INT,0},
{MN_BOL,0},
{MN_INT,0},
{MN_BOL,0},
{MN_BOL,0},
{MN_IGN,0}
};
char *RFlagMenu[] = {
"Average",
"Median",
"Typecount",
"Max",
"Min",
"Standard Deviation",
"Toggle All",
"Runs to average",
"Exit",
NULL
};
struct VDisp RFlagVals[] = {
{MN_BOL,0},
{MN_BOL,0},
{MN_BOL,0},
{MN_BOL,0},
{MN_BOL,0},
{MN_BOL,0},
{MN_IGN,0},
{MN_INT,0},
{MN_IGN,0}
};
struct NPop {
char *Name;
int Size;
int Type;
int Flags[12];
int RFlags[6];
};
struct {
char *Name;
int NumPops,CLevel;
struct NPop *Pops;
int rep,sep,flags,pass;
} Project;
char *MOTD;
int mcount(char **);
int menu(char *,char **,struct VDisp *);
int msg(char *);
int int_query(char *,int);
char *str_query(char *,char *);
int bool_query(char *,int);
void save_template(char *);
void load_template(char *);
void save_pop(FILE *,struct NPop);
void load_pop(FILE *,int);
void create_project(char *);
char *addpart(char *,char *);
void filtdump(FILE *,char *,int);
int file_write(char *);
int main(int cnt,char *arg[])
{
char z,buf[255],*tmenu[16],*p;
int i,n;
InitRand(time(NULL));
Project.Name=malloc(strlen(DEF_NAME)+1);
strcpy(Project.Name,DEF_NAME);
Project.NumPops=1;
Project.Pops = NULL;
Project.CLevel = 1;
PrVals[0].Val = (long)Project.Name;
PrVals[1].Val = 1;
PrVals[2].Val = (long)CLNames[1];
PrVals[3].Val = 0;
Project.sep=0;
Project.Pops = malloc(sizeof(struct NPop));
memset(Project.Pops,0,sizeof(struct NPop));
Project.Pops[0].Name = malloc(10);
strcpy(Project.Pops[0].Name,"Polyphant");
Project.Pops[0].Flags[4] = 1; /* Do mutate */
Project.Pops[0].Size = 20;
Project.flags=0;
Project.pass=1;
MOTD = malloc(1024);
strcpy(MOTD,"Welcome.");
switch(cnt) {
case 1:
z=msg(CLEAR "\nWelcome! This is the GAP Conjurer.\n\nConjure a skeleton? " DEFY " : ");
break;
case 2:
if(strcmp(arg[1],"-h") || strcmp(arg[1],"--help")) {
fprintf(stderr,"%s [Template] [Dest. Dir]\n",arg[0]);
goto tixE;
}
load_template(arg[1]);
z = 1;
break;
case 3:
load_template(arg[1]);
create_project(arg[2]);
goto tixE;
break;
default:
fprintf(stderr,"%s [Template] [Dest. Dir]\n",arg[0]);
goto tixE;
}
if(toupper(z)!=NO) {
do {
i=menu("Main Menu",Menu1,NULL);
switch(i) {
case 1:
do {
i = menu("Project parameters",PrParms,PrVals);
switch(i) {
case 1: /* Project Name */
free(Project.Name);
Project.Name = str_query("Project name [" DEF_NAME "] ?",DEF_NAME);
PrVals[0].Val = (long)Project.Name;
sprintf(MOTD,"Project name is now %s.",Project.Name);
break;
case 2: /* # of pops */
i = int_query("How many populations in project [1] ?",1);
if(i>0) {
for(n=0;n!=Project.NumPops;n++) {
if(Project.Pops[n].Name!=NULL) {
free(Project.Pops[n].Name);
}
}
Project.NumPops = i;
PrVals[1].Val = Project.NumPops;
if(Project.Pops!=NULL) free(Project.Pops);
Project.Pops = malloc(Project.NumPops*sizeof(struct NPop));
memset(Project.Pops,0,Project.NumPops*sizeof(struct NPop));
n=Rnd(NUM_DEFNAMES);
for(i=0;i!=Project.NumPops;i++) {
sprintf(buf,"Name for population #%d [%s]? :",i,DefNames[(i+n)%NUM_DEFNAMES]);
Project.Pops[i].Name = str_query(buf,DefNames[(i+n)%NUM_DEFNAMES]);
Project.Pops[i].Flags[4] = 1; /* Do mutate */
Project.Pops[i].Size = 20;
}
}
sprintf(MOTD,"%d populations created.",Project.NumPops);
break;
case 3: /* Comment level */
Project.CLevel = menu("Comment level",CLMenu,NULL)-1;
PrVals[2].Val = (long)CLNames[Project.CLevel];
sprintf(MOTD,"Comment level set to %s.",CLNames[Project.CLevel]);
break;
case 4:
Project.sep^=1;
PrVals[3].Val^=1;
break;
case 5:
Project.flags ^= WIZFLG_PLOT;
PrVals[4].Val = Project.flags&WIZFLG_PLOT;
break;
case 6:
Project.flags ^= WIZFLG_PSPLOT;
PrVals[5].Val = Project.flags&WIZFLG_PSPLOT;
break;
}
} while(i!=mcount(PrParms));
i=1;
break;
case 2: /* Pop params */
if(Project.NumPops>0) {
do {
if(Project.NumPops>1) {
for(i=0;i!=Project.NumPops;i++) {
tmenu[i] = Project.Pops[i].Name;
}
tmenu[i] = "Exit";
tmenu[i+1] = 0;
n = menu("Select Population",tmenu,NULL)-1;
if(n==(mcount(tmenu)-1)) {
break;
}
} else {
n = 0;
}
PoVals[0].Val = (long)Project.Pops[n].Name;
PoVals[1].Val = Project.Pops[n].Size;
PoVals[2].Val = (long)GenomeNames[Project.Pops[n].Type];
do {
sprintf(buf,"Parameters for %s population",Project.Pops[n].